# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=coreutils
pkgver=8.31
pkgrel=1
pkgdesc="The basic file, shell and text manipulation utilities of the GNU operating system"
arch=('i686' 'x86_64')
license=('GPL3')
url="https://www.gnu.org/software/coreutils"
groups=('base')
depends=('gmp' 'libiconv' 'libintl')
makedepends=('gmp-devel' 'libiconv-devel' 'gettext-devel' 'help2man')
install=${pkgname}.install
source=(https://ftp.gnu.org/gnu/${pkgname}/${pkgname}-${pkgver}.tar.xz{,.sig}
        001-coreutils-8.30.patch)
sha256sums=('ff7a9c918edce6b4f4b2725e3f9b37b0c4d193531cac49a48b56c4d0d3a9e9fd'
            'SKIP'
            'f246efcc7638db9c9c728e88c3a53d957953033ed650f03b0405e81ba9f5960a')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p1 -i ${srcdir}/001-coreutils-8.30.patch

  autoreconf -fi
  sed -i -e 's|\(libstdbuf\.so\)$(EXEEXT)|\1|g' Makefile.in

  #make test cases work
  sed -i "s|case \$perms in drwx--\[-S\]---\*|case \$perms in drwx\*|" tests/init.sh

}

build() {
  [[ -d ${srcdir}/build-${CHOST} ]] && rm -rf ${srcdir}/build-${CHOST}
  mkdir -p ${srcdir}/build-${CHOST} && cd ${srcdir}/build-${CHOST}

  ../${pkgname}-${pkgver}/configure \
    --build=${CHOST} \
    --prefix=/usr \
    --libexecdir=/usr/lib \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --program-transform-name=s/kill/gkill/ \
    --without-libintl-prefix \
    --without-libiconv-prefix \
    --enable-install-program=arch,hostname \
    --enable-no-install-program=uptime \
    gl_cv_func_strtod_works=yes \
    gl_cv_func_strtold_works=yes

  make
}

check() {
  cd ${srcdir}/build-${CHOST}
  make check
}

package() {
  cd ${srcdir}/build-${CHOST}
  make DESTDIR=${pkgdir} install

  test ! -f ${pkgdir}/usr/lib/coreutils/libstdbuf.so.exe ||
  mv ${pkgdir}/usr/lib/coreutils/libstdbuf.so.exe \
    ${pkgdir}/usr/lib/coreutils/libstdbuf.so

  #mkdir -p ${pkgdir}/usr/etc
  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/src/dircolors.hin ${pkgdir}/etc/DIR_COLORS
}
